#!/bin/bash

if [ $(uname -s) != 'Linux' ];then
		printf \
		'
			{	
				"messageCode": "error",
				"message": "unknow os type",
				"data": []
			}\n'
    exit 1
fi


function linux_host_check() {
    #局部变量定义
    local result=""
    local err_msg=""

    #检测sudo权限
    function sudo_check() {
        if [ `sudo -A ls 1>/dev/null 2>&1;echo $?` -ne 0 ];then
            return_error HAVE_NOT_SUDO_AUTH
            exit 1
        fi
    }

    #获取主机名
    function host_name() {
        host_name=`uname -n 2>/dev/null`
        if [ $? -ne 0 ]; then
            err_msg="GET_HOSTNAME_FAILURE"
        fi
        if [ "$1" = "err_msg" ];then
            echo "${err_msg}"
            return 1
        else
            if [ "$host_name" ];then
                echo "${host_name}"
                return 0
            else
                return 1
            fi
        fi
    }

    #IP
    function ipaddr() {
        if [ `uname` = "Linux" ]; then
            ips=(`hostname -I`)
            if [ $? -ne 0 ]; then
                err_msg="IP_TOOL_NOT_FOUND"
            fi
        else
            err_msg="UNKNOWN_OS"
        fi
        if [ "$1" = "err_msg" ];then
            echo "$err_msg"
            return 1
        else
            if [ "${ips}" ]; then
                echo ${ips[0]}
                return 0
            else
                return 1
            fi
        fi
    }

    #资产端口
    function open_port() {
		which nmap 2>/dev/null
		if [ $? -ne 0 ]; then
			err_msg="NOT_FOUND_NMAP"
		else
			ssh_port=`nmap 127.0.0.1 | grep ssh | grep open | awk -F '/' '{print $1}'`
		fi
		if [ "$1" = "err_msg" ];then
            echo "$err_msg"
            return 1
		else
			if [ "${ssh_port}" ]; then
				echo ${ssh_port}
				return 0
			else
				return 1
			fi
		fi
    }

    #获取VIP
    function ipaddr_vip() {
        if [ `uname` = "Linux" ]; then
            ips=(`hostname -I`)
            if [ $? -ne 0 ]; then
                err_msg="IP_TOOL_NOT_FOUND"
            fi
        else
            err_msg="UNKNOWN_OS"
        fi
        if [ "$1" = "err_msg" ];then
            echo "$err_msg"
            return 1
        else
            if [ "${ips}" ]; then
                echo ${ips[1]}
                return 0
            else
                return 1
            fi
        fi
    }

    #开放协议
    function open_protocol() {
		which nmap 2>/dev/null
		if [ $? -ne 0 ]; then
			err_msg="NOT_FOUND_NMAP"
		else
			open_protocol=`nmap 127.0.0.1 | grep open| grep -v unknown | awk '{print $NF}'`
		fi
		if [ "$1" = "err_msg" ];then
            echo "$err_msg"
            return 1
		else
			if [ "${open_protocol}" ]; then
				echo ${open_protocol}
				return 0
			else
				return 1
			fi
		fi
    }

    #IP类型0为IPV4，1为IPV6。z2
    function ip_type() {
        myip=ipaddr
        if [ "$1" = "err_msg" ];then
            echo "$err_msg"
            return 1
        else
			if [ "$myip" != "${1#*[0-9].[0-9]}" ]; then
				echo "IPV4"
			elif [ "$myip" != "${1#*:[0-9a-fA-F]}" ]; then
				echo "IPV6"
			else
				echo "Unrecognized IP format '$myip'"
			fi
        fi
    }

	#前端IP[必有，10.1,10.2,10.98开头，如有多个，则对应的IP与主机名写到/etc/hosts的为前端IP]
	function get_qdIp {
	    ip_src=`/sbin/ip addr 2>/dev/null|awk 'BEGIN{FS="/| "};/global/&&!/secondary/{print $6}'|egrep -v '^169.*|^127.*|^10.131.*'|sort |grep ^10|xargs`
		if [ $? -ne 0 ];then
			err_msg="GET_QDIP_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$ip_src" ];then
			    ip_arry=(${ip_src})
				for ((i=0;i<${#ip_arry[*]};i++))
				do
					if [[ $(grep -i `hostname` /etc/hosts |grep ${ip_arry[$i]} |grep -v \# |wc -l) -ge 0 ]];then
						echo "${ip_arry[$i]}"
						return 0
					fi
				done
			else
				return 1
			fi
		fi
	}

    #资产类型
	function asset_type() {
		if [[ `/sbin/lspci 2>/dev/null|grep -i 'vmware'` ]];then
			hostType="私有云虚拟机"
		elif [[ `get_qdIp |grep "10.98"` ]];then
		    hostType="公有云虚拟机"
		elif [[ 'uname' == "AIX" ]];then
		    hostType="小型机分区"
		else
		    hostType="物理机"
		fi
		
		if [ $? -ne 0 ];then
			err_msg="GET_ASSET_TYPE_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$hostType" ];then
				echo "$hostType"
				return 0
			else
				return 1
			fi
		fi
		
	}

    #资产型号
    function information_model() {
        model=`dmidecode -s system-product-name`
        if [ $? -ne 0 ];then
            err_msg="GET_INFO_MODEL_FAILURE"
        fi
        if [ "$1" == "err_msg" ]; then
            echo $err_msg
            return 1
        else
            if [ "$model" ]; then
                echo "$model"
                return 0
            else
                echo "unkown"
                return 1
            fi
        fi
    }

    #厂商名称
    function brand_manufacturer() {
        changshang=`dmidecode -s system-manufacturer`
        if [ $? -ne 0 ];then
            err_msg="GET_BRAND_MANU_FAILURE"
        fi
        if [ "$1" == "err_msg" ]; then
            echo $err_msg
            return 1
        else
            if [ "$changshang" ]; then
                echo "$changshang"
                return 0
            else
                echo "unkown"
                return 1
            fi
        fi
    }

    #CPU厂商
    function cpu_manufacturer() {
        cpu_changshang=`cat /proc/cpuinfo |grep "vendor_id" | awk '{print $NF}'| head -1`
        if [ $? -ne 0 ];then
			err_msg="GET_CPU_MANU_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$cpu_changshang" ];then
			    echo "$cpu_changshang"
				return 0
			else
				return 1
			fi
		fi
    }

    #服务器类型
    function server_type() {
        servertype=`dmesg | grep -i virtual`
		if [ $? -ne 0 ];then
			err_msg="GET_SERVER_TYPE_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$servertype" ];then
			    if [ "$servertype" ];then
				    echo "虚拟机"
					return 0
				else
				    echo "unkown"
					return 1
				fi	
			else
				return 1
			fi
		fi
    }

	#默认网关
	function get_gateway() {
		gateway=`/sbin/ip route show  2> /dev/null|grep default`
		if [ $? -ne 0 ];then
			err_msg="GET_GATEWAY_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$gateway" ];then
				echo "$gateway"|awk '{print $3}'
				return 0
			else
				return 1
			fi
		fi
	}

    #内存厂商
    function memory_manufacturerr() {
        memory_changshang=_`dmidecode | grep -C 16 'Speed:'  | grep -A 16 "Memory Device" | grep 'Manufacturer:' | awk '{for (i=2;i<=NF;i++)printf("%s ", $i);print ""}' | head -1`       
        if [ $? -ne 0 ];then
			err_msg="GET_MEMORY_MANU_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$memory_changshang" ];then
				echo "$memory_changshang"
				return 0
			else
				return 1
			fi
		fi
    }

    #获取操作系统版本
	function get_os_version() {
		if [ -s /etc/oracle-release ]; then
			os_version=$(cat /etc/oracle-release 2>/dev/null)
		elif [ -s /etc/SuSE-release ]; then
			os_version=$(cat /etc/SuSE-release 2>/dev/null|grep ^SUSE)
		elif [ -f /etc/redhat-release ]; then
			os_version=$(cat /etc/redhat-release 2>/dev/null)
		elif [ -s /etc/redhat-release ]; then
			os_version=$(cat /etc/centos-release 2>/dev/null)
		elif [ -r /etc/os-release ]; then
			grep 'NAME="Ubuntu"' /etc/os-release >/dev/null 2>&1
			if [ $? == 0 ]; then
				os_version=Ubuntu
			fi
		else
			os_version="Unknown Linux"
		fi	
		if [ $? -ne 0 ];then
			err_msg="GET_OS_VERSION_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$os_version" ];then
				echo "$os_version"
				return 0
			else
				return 1
			fi
		fi
	}

	#获取操作系统类型
	function get_os_type() {
		os_type=`uname -s 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_OS_TYPE_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$os_type" ];then
				echo "$os_type"
				return 0
			else
				return 1
			fi
		fi
	}

    #内存型号名称
    function memory_model() {
        mem_type=`dmidecode | grep -C 16 'Speed:'  | grep -A 16 "Memory Device" | grep 'Type:' | awk '{print $2}' | sort | uniq`
        if [ $? -ne 0 ];then
			err_msg="GET_MEM_TYPE_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$mem_type" ];then
				echo "$mem_type"
				return 0
			else
				return 1
			fi
		fi
    }

    #内核版本
	function get_kernel_version() {
		kernel_version=`uname -r 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_KERNEL_VERSION_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$kernel_version" ];then
				echo "$kernel_version"
				return 0
			else
				return 1
			fi
		fi
	}

    #内存容量(MB)
	function get_memory_total() {
		memory_total=`cat /proc/meminfo 2>/dev/null| grep MemTotal |awk '{sum=$2/1024};{printf ("%.0f\n", sum)}'`
		if [ $? -ne 0 ];then
			err_msg="GET_MEMORY_TOTAL_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$memory_total" ];then
				echo "$memory_total"
				return 0
			else
				return 1
			fi
		fi
	}

#磁盘获取相关函数
	function disk_size {
		for ((i = 0; i < $len; i++)); do
			if [ "${disk_arry[$i]}" == 'cciss!c0d0' ]; then
				cciss_add $1
			else
				disk_type ${disk_arry[$i]}
			fi
		done
		if [ "$1" == "data" ]; then
			add_size data
		elif [ "$1" == "sys" ]; then
			add_size sys
		else
			return 1
		fi
	}
	function cciss_add {
		z=$(lsblk -nbd | awk '{sum += $4};END {print sum}')
		s=$(lsblk -nbd | grep -e ^'cciss!c0d0' | awk '{print $4}')
		d=$(expr $z - $s)
		if [ "$1" == "data" ]; then
			echo | awk '{printf ("%1.f",'$d'/1024/1024/1024)}'
			return 0
		fi
		if [ "$1" == "sys" ]; then
			echo | awk '{printf ("%1.f",'$s'/1024/1024/1024)}'
			return 0
		fi
	}
	function disk_type() {
		if [ "$1" != "sr0" ] && [ "$1" != "loop0" ] && [ "$1" != 'cciss!c0d0' ] && [[ "$1" =~ ^"Vx" ]]; then
			num="-2"
		fi
		if [ "$1" == 'cciss!c0d0' ]; then
			num=1
		fi
		if [ "$1" == "sr0" ] || [ "$1" == "loop0" ]; then
			num=-1
		else
			num=$(lsblk -n /dev/$1 | grep -c /$)
		fi
		if [ "$num" == "-2" ]; then
			list_size "data" $1
		fi
		if [ "$num" == "0" ]; then
			mp_check $1
			t=$?
			if [ "$t" == "1" ]; then
				list_size "data" $1
			elif [ "$t" == "2" ]; then
				list_size "data" $1
			fi
		elif [ $num -ge 1 ]; then
			list_size "sys" $1
		fi
	}
	function list_size() {
		case $1 in
		"data")
			if [[ "$2" =~ ^"Vx". ]]; then
				s=$(lsblk -nb | grep $2 | grep ^[a-zA-Z] | awk '{print $4}')
			else
				s=$(lsblk -nlb /dev/$2 | head -1 | awk '{print $4}')
			fi
			data_arry[${#data_arry[*]}]=$s
			;;
		"sys")
			if [ "$2" == 'cciss!c0d0' ]; then
				s=$(lsblk -nb | grep $2 | grep ^[a-z] | awk '{print $4}')
			else
				s=$(lsblk -nb /dev/$2 | head -1 | awk '{print $4}')
			fi
			sys_arry[${#sys_arry[*]}]=$s
			;;
		*)
			return 1
			;;
		esac
	}
	function mp_check() {
		res=$(lsblk -nbl /dev/$1 | grep mpath. | awk '{print $1}')
		if [ "$res" != "" ]; then
			for ((n = 0; n < $len_mp; n++)); do
				if [ "$res" == "${mp_arry[$n]}" ]; then
					unset mp_arry[$n]
					return 1
					break
				fi
			done
		else
			return 2
		fi
	}
	function add_size() {
		data_size=0
		sys_size=0
		if [ "$1" == "data" ]; then
			for i in ${data_arry[*]}; do
				data_size=$(expr $i + $data_size)
			done
			echo | awk '{printf ("%1.f",'$data_size'/1024/1024/1024)}'
		else
			for i in ${sys_arry[*]}; do
				sys_size=$(expr $i + $sys_size)
			done
			echo | awk '{printf ("%1.f",'$sys_size'/1024/1024/1024)}'
		fi
	}

    #获取磁盘名称并赋值
	disk_name=$(lsblk -nb 2>/dev/null| awk '{print $1}' | grep ^[a-zA-Z])
	disk_arry=($disk_name)
	len=${#disk_arry[*]}
	sys_arry=()
	data_arry=()
	mp_arry=($(lsblk -nbl 2>/dev/null| grep mpath. | awk '{print $1}' | awk '!a[$1]++{print}'))
	len_mp=${#mp_arry[*]}

    #操作系统磁盘空间(GB)
    function get_disk_size() {
		which lsblk  1>/dev/null 2>&1
		if [ $? -ne 0 ];then
			err_msg="GET_DISK_SIZE_FAILURE"
		else
			disk_sys=$(disk_size sys)
			if [ $? -ne 0 ];then
				err_msg="GET_DISK_SIZE_FAILURE"
			fi			
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$disk_sys" ];then
				echo "$disk_sys"
				return 0
			else
				return 1
			fi
		fi		
	}
    #数据盘磁盘空间(GB)
	function get_datadisk_size() {
		which lsblk  1>/dev/null 2>&1
		if [ $? -ne 0 ];then
			err_msg="GET_DISK_SIZE_FAILURE"
		else
			disk_data=$(disk_size data)
			if [ $? -ne 0 ];then
				err_msg="GET_DATADISK_SIZE_FAILURE"
			fi
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$disk_data" ];then
				echo "$disk_data"
				return 0
			else
				return 1
			fi
		fi	
	}
    #开源 0-否 1-是
    function opensource() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo "开源"
			return 0
		fi
    }
    #CPU型号
	function get_cpu_model() {
		cpu_model=`cat /proc/cpuinfo 2> /dev/null| grep name`
		if [ $? -ne 0 ];then
			err_msg="GET_CPU_MODEL_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$cpu_model" ];then
				echo "$cpu_model"| cut -f2 -d: | uniq  | sed 's/^[ \t]*//g' | awk '{print $2,$3,$4,$5}'
				return 0
			else
				return 1
			fi
		fi		
	}
	#获取CPU个数
	function get_cpu_number() {
		cpu_number=`cat /proc/cpuinfo  2> /dev/null| grep "cpu cores"`
		if [ $? -ne 0 ];then
			err_msg="GET_CPU_NUMBER_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$cpu_number" ];then
				echo "$cpu_number"| uniq | awk -F':' '{print $2}' |xargs
				return 0
			else
				return 1
			fi
		fi		
	}
	#获取CPU核数
	function get_cpu_kernel_number() {
		cpu_kernel_number=`cat /proc/cpuinfo  2> /dev/null| grep processor`
		if [ $? -ne 0 ];then
			err_msg="GET_CPU_KERNEL_NUMBER_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$cpu_kernel_number" ];then
				echo "$cpu_kernel_number" | wc -l
				return 0
			else
				return 1
			fi
		fi		
	}
	#获取CPU主频
	function get_cpu_frequency() {
		cpu_frequency=`cat /proc/cpuinfo 2> /dev/null| grep "model name"`
		if [ $? -ne 0 ];then
			err_msg="GET_CPU_FREQUENCY_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$cpu_frequency" ];then
				echo "$cpu_frequency" | awk '{print $NF}' |uniq
				return 0
			else
				return 1
			fi
		fi			
	}

	#获取SWAP大小(GB)
	function get_swap_size() {
		swap_size=`grep -i SwapTotal /proc/meminfo 2>/dev/null |awk '{sum=$2/1024/1024};{printf ("%.2f\n", sum)}'`
		if [ $? -ne 0 ];then
			err_msg="GET_SWAP_SIZE_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$swap_size" ];then
				echo "$swap_size"
				return 0
			else
				return 1
			fi
		fi			
	}
    #网络位置,0-内网，1-外网
    function net_position() {
        ping -c 2 $ip 2>/dev/null
		if [ $? -ne 0 ];then
			err_msg="GET_NET_POSITION_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
            echo "内网"
            return 0
        fi
    }
    #交换芯片厂商名称,参见B.8厂商名称代码表，如无请填写实际厂商名称
    function chip_manufacturer() {
        chip_manu=`dmidecode | grep -A2 "Base Board Information" | grep Manufacturer | awk -F: '{print $2}'`
        if [ $? -ne 0 ];then
			err_msg="GET_CHIP_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$chip_manu" ];then
				echo "$chip_manu"
				return 0
			else
				return 1
			fi
		fi
    }
    #交换芯片型号名称,参见B.9资产型号代码表，如无请填写实际资产型号
    function chip_model() {
        chip_name=`dmidecode | grep -A2 "Base Board Information" | grep "Product Name" | awk -F: '{print $2}'`
        if [ $? -ne 0 ];then
			err_msg="GET_CHIP_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$chip_name" ];then
				echo "$chip_name"
				return 0
			else
				return 1
			fi
		fi
    }

    #语言
    function language() {
		language=`echo $LANG`
		if [ $? -ne 0 ];then
			err_msg="GET_LANGUAGE_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$language" ];then
				echo "$language"
				return 0
			else
				return 1
			fi
		fi
    }


    # 资产状态
    #0-  下线：表示资产不使用，但资产仍然存在
    #1-  在网：表示资产正在运行使用
    #2-  停用：表示资产当前停止运行
    #3-  删除：表示资产不存在
    function access_state() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
				echo "在网"
				return 0
		fi
    }
    #使用状态
    function use_state() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
				echo "上线"
				return 0
		fi
    }
    #资产发现方式0-远程扫描，1-WEB爬虫，2-网络流量分析，3-日志分析，4-Agent，5-登录资产，6-手动导入，7-其他
    function found_typelist() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
				echo "Agent"
				return 0
		fi
    }
    #资产发现时间
    function found_type_time() {
        now_time=`date '+%Y%m%d%H%M%S'`
		if [ $? -ne 0 ];then
			err_msg="GET_NOW_TIME_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$now_time" ];then
				echo "$now_time"
				return 0
			else
				return 1
			fi
		fi

    }
    #是否上报,是、否、待上报
    function is_report() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
				echo "待上报"
				return 0
		fi
    }
    #环境, 生产、容灾、测试
    function environment() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
				echo "生产"
				return 0
		fi
    }

    #获取设备序列号
	function get_serial_number {
		serial_number=`/usr/sbin/dmidecode 2>/dev/null | grep 'Serial Number'`
		if [ $? -ne 0 ];then
			err_msg="GET_SERIAL_NUMBER_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$serial_number" ];then
				echo "$serial_number"| awk -F: '{print $2}' | head -1
				return 0
			else
				return 1
			fi
		fi
	}

    #是否打印逗号
	function print_comma {
		if [ $1 -ne $2 ]; then
			echo ","
		fi
	}

	#打印结果
	function return_result {
		printf \
		'
		{	
			"messageCode": "success",
			"message": "",
			"data": [
		'
		host_names=($(host_name))
		num=${#host_names[@]}
		i=0
		#打印每个实例
		for host_name in ${host_names[@]}	
		do
			((i++))
			echo \
			'{
				"ciCode": "host",
				"attributes": [
					{
					    "attrName": "主机名",
					    "attrCode": "host_name",
					    "attrValue": "'`host_name`'",
					    "attrFailureReason": "'`host_name err_msg`'"
					},{
						"attrName": "IP",
						"attrCode": "ip",
						"attrValue": "'`ipaddr`'",
						"attrFailureReason": "'`ipaddr err_msg`'"		
					},{
						"attrName": "资产端口",
						"attrCode": "open_port",
						"attrValue": "'`open_port`'",
						"attrFailureReason": "'`open_port err_msg`'"		
					},{
						"attrName": "VIP",
						"attrCode": "vip",
						"attrValue": "'`ipaddr_vip`'",
						"attrFailureReason": "'`ipaddr_vip err_msg`'"		
					},{
						"attrName": "开放协议",
						"attrCode": "protocol",
						"attrValue": "'`open_protocol`'",
						"attrFailureReason": "'`open_protocol err_msg`'"		
					},{
						"attrName": "IP类型",
						"attrCode": "ip_type",
						"attrValue": "'`ip_type`'",
						"attrFailureReason": "'`ip_type err_msg`'"		
					},{
						"attrName": "资产类型",
						"attrCode": "asset_type",
						"attrValue": "'`asset_type`'",
						"attrFailureReason": "'`asset_type err_msg`'"		
					},{
						"attrName": "厂商名称",
						"attrCode": "brand_manufacturer",
						"attrValue": "'`brand_manufacturer`'",
						"attrFailureReason": "'`brand_manufacturer err_msg`'"		
					},{
						"attrName": "资产型号",
						"attrCode": "information_model",
						"attrValue": "'`information_model`'",
						"attrFailureReason": "'`information_model err_msg`'"		
					},{
						"attrName": "使用状态",
						"attrCode": "use_state",
						"attrValue": "'`use_state`'",
						"attrFailureReason": "'`use_state err_msg`'"		
					},{
						"attrName": "CPU厂商",
						"attrCode": "cpu_manufacturer",
						"attrValue": "'`cpu_manufacturer`'",
						"attrFailureReason": "'`cpu_manufacturer err_msg`'"		
					},{
						"attrName": "服务器类型",
						"attrCode": "server_type",
						"attrValue": "'`server_type`'",
						"attrFailureReason": "'`server_type err_msg`'"		
					},{
						"attrName": "默认网关",
						"attrCode": "gateway",
						"attrValue": "'`get_gateway`'",
						"attrFailureReason": "'`get_gateway err_msg`'"		
					},{
						"attrName": "内存厂商",
						"attrCode": "memory_manufacturerr",
						"attrValue": "'`memory_manufacturerr`'",
						"attrFailureReason": "'`memory_manufacturerr err_msg`'"		
					},{
						"attrName": "操作系统版本",
						"attrCode": "os_version",
						"attrValue": "'`get_os_version`'",
						"attrFailureReason": "'`get_os_version err_msg`'"		
					},{
						"attrName": "操作系统类型",
						"attrCode": "os_type",
						"attrValue": "'`get_os_type`'",
						"attrFailureReason": "'`get_os_type err_msg`'"		
					},{
						"attrName": "内存型号名称",
						"attrCode": "memory_model",
						"attrValue": "'`memory_model`'",
						"attrFailureReason": "'`memory_model err_msg`'"		
					},{
						"attrName": "内核版本",
						"attrCode": "kernel_version",
						"attrValue": "'`get_kernel_version`'",
						"attrFailureReason": "'`get_kernel_version err_msg`'"		
					},{
						"attrName": "内存容量(MB)",
						"attrCode": "memory",
						"attrValue": "'`get_memory_total`'",
						"attrFailureReason": "'`get_memory_total err_msg`'"		
					},{
						"attrName": "交换芯片厂商名称",
						"attrCode": "chip_manufacturer",
						"attrValue": "'`chip_manufacturer`'",
						"attrFailureReason": "'`chip_manufacturer err_msg`'"		
					},{
						"attrName": "交换芯片型号名称",
						"attrCode": "chip_model",
						"attrValue": "'`chip_model`'",
						"attrFailureReason": "'`chip_model err_msg`'"		
					},{
						"attrName": "操作系统磁盘空间(GB)",
						"attrCode": "disk_size",
						"attrValue": "'`get_disk_size`'",
						"attrFailureReason": "'`get_disk_size err_msg`'"		
					},{
						"attrName": "开源",
						"attrCode": "opensource",
						"attrValue": "'`opensource`'",
						"attrFailureReason": "'`opensource err_msg`'"		
					},{
						"attrName": "数据盘磁盘空间(GB)",
						"attrCode": "datadisk_size",
						"attrValue": "'`get_datadisk_size`'",
						"attrFailureReason": "'`get_datadisk_size err_msg`'"		
					},{
						"attrName": "语言",
						"attrCode": "language",
						"attrValue": "'`language`'",
						"attrFailureReason": "'`language err_msg`'"		
					},{
						"attrName": "CPU型号",
						"attrCode": "cpu_model",
						"attrValue": "'`get_cpu_model`'",
						"attrFailureReason": "'`get_cpu_model err_msg`'"		
					},{
						"attrName": "资产状态",
						"attrCode": "access_state",
						"attrValue": "'`access_state`'",
						"attrFailureReason": "'`access_state err_msg`'"		
					},{
						"attrName": "CPU个数",
						"attrCode": "cpu_number",
						"attrValue": "'`get_cpu_number`'",
						"attrFailureReason": "'`get_cpu_number err_msg`'"		
					},{
						"attrName": "CPU核数",
						"attrCode": "cpu_kernel_number",
						"attrValue": "'`get_cpu_kernel_number`'",
						"attrFailureReason": "'`get_cpu_kernel_number err_msg`'"		
					},{
						"attrName": "SWAP大小(GB)",
						"attrCode": "swap_size",
						"attrValue": "'`get_swap_size`'",
						"attrFailureReason": "'`get_swap_size err_msg`'"		
					},{
						"attrName": "网络位置",
						"attrCode": "net_position",
						"attrValue": "'`net_position`'",
						"attrFailureReason": "'`net_position err_msg`'"		
					},{
						"attrName": "资产发现方式",
						"attrCode": "found_typelist",
						"attrValue": "'`found_typelist`'",
						"attrFailureReason": "'`found_typelist err_msg`'"		
					},{
						"attrName": "资产发现时间",
						"attrCode": "found_type_time",
						"attrValue": "'`found_type_time`'",
						"attrFailureReason": "'`found_type_time err_msg`'"		
					},{
						"attrName": "是否上报",
						"attrCode": "is_report",
						"attrValue": "'`is_report`'",
						"attrFailureReason": "'`is_report err_msg`'"		
					},{
						"attrName": "环境",
						"attrCode": "environment",
						"attrValue": "'`environment`'",
						"attrFailureReason": "'`environment err_msg`'"		
					},{
						"attrName": "CPU主频",
						"attrCode": "cpu_ghz",
						"attrValue": "'`get_cpu_frequency`'",
						"attrFailureReason": "'`get_cpu_frequency err_msg`'"		
					}
				],
				"relations": [ 
					{
						"quoteCiCode": "x86_server",
						"relationName": "安装于",
						"quoteInstances": [
							[
								{
									"attrName": "序列号",
									"attrCode": "serial_number",
									"attrValue": "'`get_serial_number`'",
									"attrFailureReason": "'`get_serial_number err_msg`'"
								}
							]
						]
					},{
						"quoteCiCode": "minicomputer",
						"relationName": "安装于",
						"quoteInstances": [
							[
								{
									"attrName": "序列号",
									"attrCode": "serial_number",
									"attrValue": "'`get_serial_number`'",
									"attrFailureReason": "'`get_serial_number err_msg`'"
								}
							]
						]
					}
				]
			}'`print_comma $i $num`''
		done
		echo "]}"
	}
	return_result;

}




#错误定义
function return_error() {
	case $1 in 
	'UNKNOW_OS')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "unknow os type",
				"data": []
			}\n'
		;;
	'HAVE_NOT_SUDO_AUTH')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "have not sudo auth",
				"data": []
			}\n'
		;;
	'NO_INSTANCE')
		printf \
		'
			{	
				"messageCode": "success",
				"message": "no database",
				"data": []
			}\n'
		;;
	'CMD_EXECUTE_ERROR')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "ps execute error",
				"data": []
			}\n'
		;;
	esac
}


#自发现入口
function main() {
	os_type=`uname -s`
	case ${os_type} in
		'Linux')
			linux_host_check
			;;
		'AIX')
			return_error 'UNKNOW_OS';
			;;
		'*')
			return_error 'UNKNOW_OS';
			;;
	esac
}

main
